-----------------------------------------------------------------------------
-- 羁绊(属性)加成界面
--
---------------
-- Author: wd
-----------------------------------------------------------------------------
require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIFetterInfluence = {};
HelperFunc.SetUIEnv(UIFetterInfluence)
local this = UIFetterInfluence;

this.hasInit = false;
this.btnClose = nil
this.elementList = nil
function UIFetterInfluence.Init()
    local name, packageName = getUIName(PanelResNames.UIFetterInfluence)
    panelMgr:ShowUI(name, packageName, this.OnCreate, Config.useLocalUI)
end

function UIFetterInfluence.OnCreate(obj)
    if not hasInit then
        hasInit = true;
        btnClose = obj

        elementList = obj:GetChild("attrGroup")
        this.title = obj:GetChild("title")
        this.tips = obj:GetChild("tips")
        this.title.text = getLanguage("TrammelBonus_Title")
        this.tips.text = getLanguage("TrammelBonus_Des")
        this.addEvent();
    end

    this.CalculateAllAttr()
end

function UIFetterInfluence.addEvent()
    btnClose.onClick:Add(this.onClickBack)
end

function UIFetterInfluence.removeEvent()
    btnClose.onClick:Clear()
end


function UIFetterInfluence.OnDestroy()
    hasInit = false
    this.removeEvent();
end

function UIFetterInfluence.OnDisable()
end

--- 计算羁绊属性
function UIFetterInfluence.CalculateAllAttr()
    local tableHeros = logicMgr.HeroManager.getOwnerHeroList(false,nil,true)

    local fetterAttrList = {}
    for _, heroInfo in pairs(tableHeros) do
        if heroInfo["fetter"] then
            for _, fetterID in pairs(heroInfo["fetter"]) do
                local cfgInfo = G.dataTable["t_trammel"][tostring(fetterID)]
                if cfgInfo then

                    local attrList = decodeJsonStrToTabel(cfgInfo["f_AttributeBonus"])
                    if attrList then
                        for _, v in pairs(attrList) do
                            local attrId = v[1]
                            local attrNum = v[2]
                            if fetterAttrList[attrId] == nil then
                                fetterAttrList[attrId] = {attrId = attrId, attrNum = attrNum}
                            else
                                fetterAttrList[attrId] = {attrId = attrId, attrNum = fetterAttrList[attrId]["attrNum"] + attrNum}
                            end
                        end
                    end
                end
            end
        end
    end

    -- 默认显示攻防血
    if #fetterAttrList < 1 then
        fetterAttrList[1] = {attrId = 1, attrNum = 0}
        fetterAttrList[2] = {attrId = 2, attrNum = 0}
        fetterAttrList[3] = {attrId = 3, attrNum = 0}
    end
    local function UpdateAttr(_index, atrObj)
        local key = _index + 1

        local attrInfo = fetterAttrList[key]
        local attrID = attrInfo["attrId"]
        local value = attrInfo["attrNum"]
        local attrSingle = G.dataTable["t_attributelibrary"][tostring(attrID)]

        local attrNumStr= attrSingle.f_AttributeType == 1 and string.format("%d",value) or string.format("%s%%",tostring(value/100))
        local txtAttrValue = atrObj:GetChild("number")
        txtAttrValue.text = attrNumStr

        local icon = atrObj:GetChild("Icon")
        icon.url = EnumConst.AtlasPath.ICONAtlas.."attr"..attrID

        local attrName = atrObj:GetChild("Attr")
        attrName.text = attrSingle.f_AttributeName

        local imgContro= atrObj:GetController("c1")
        imgContro.selectedIndex = ((key % 2) == 1) and 0 or 1
    end

    elementList.itemRenderer = UpdateAttr
    elementList.numItems = table.nums(fetterAttrList)
end

function UIFetterInfluence.onClickBack()
    closeUI(PanelNames.UIFetterInfluence)
end

return UIFetterInfluence;
